﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using JN.Utility;
using Power.Common;
using Power.BLL;
namespace Power.UserInfo
{
    public partial class ChangePassword : DevExpress.XtraEditors.XtraForm
    {
        #region property
        Sys_userBLL userBLL = new Sys_userBLL();
        private string _user_id;
        private string _dept_id;

        public string User_id
        {
            get { return _user_id; }
            set { _user_id = value; }
        }
        public string Dept_id
        {
            get { return _dept_id; }
            set { _dept_id = value; }
        }

        #endregion
        public ChangePassword()
        {
            InitializeComponent();
        }

        private void ChangePassword_Load(object sender, EventArgs e)
        {
            //initalize ui 
            if (string.IsNullOrEmpty(_user_id))
            {
                XtraMessageBox.Show("获取用户信息错误!!","错误");
                txtOldPwd.Enabled = false;
                txtPwd.Enabled = false;
                txtPwd2.Enabled = false;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //change pwd
            try
            {
                if (!IsInvalid())
                {                    
                    return;
                }
                if (ChangeUserPassword())
                {
                    XtraMessageBox.Show("修改密码成功!!", "提示");
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show("修改密码失败!!", "提示");
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
                SystemLogger.Write(ex);
            }
        }
        /// <summary>
        /// 验证密码
        /// </summary>
        /// <returns></returns>
        private bool IsInvalid()
        {
            try
            {
                bool ret = false;
                if (string.IsNullOrEmpty(txtOldPwd.Text))
                {
                    XtraMessageBox.Show("请输入旧密码!!", "提示");
                    txtOldPwd.Focus();
                    return ret;
                }
                if (string.IsNullOrEmpty(txtPwd.Text))
                {
                    XtraMessageBox.Show("请输入新密码!!", "提示");
                    txtPwd.Focus();
                    return ret;
                }
                if (string.IsNullOrEmpty(txtPwd2.Text))
                {
                    XtraMessageBox.Show("请输入确认密码!!", "提示");
                    txtPwd2.Focus();
                    return ret;
                }
                if (txtPwd.Text != txtPwd2.Text)
                {
                    XtraMessageBox.Show("二次输入的密码不一致!!", "提示");
                    txtPwd.Focus();
                    return ret;
                }

                Model.Sys_user mUser = userBLL.GetModel(_user_id);
                if (mUser == null) return ret;
                if (Common.DEncrypt.DEncrypt.Encrypt(txtOldPwd.Text) != mUser.user_password)
                {
                    XtraMessageBox.Show("旧密码输入不正确", "提示");
                    return ret;
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        private bool ChangeUserPassword()
        {
            try
            {
                bool ret = false;
                
                Model.Sys_user mUser = userBLL.GetModel(_user_id);
                if (mUser == null) return ret;

                mUser.user_password = Common.DEncrypt.DEncrypt.Encrypt(txtPwd.Text);
                ret = userBLL.Update(mUser);

                return ret;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}